/*
 * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.xiaominfo.com.
 * Developer Web Site: http://open.xiaominfo.com.
 */

package com.imut.oa.config;

import cn.hutool.core.util.RandomUtil;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/***
 * 创建Swagger配置
 * @since:knife4j-springdoc-openapi-demo 1.0
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a> 
 * 2020/03/15 20:40
 */
@Configuration
@Slf4j
public class SwaggerConfiguration {
    /**
     * 根据@Tag 上的排序，写入x-order
     *
     * @return the global open api customizer
     */
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            if (openApi.getTags()!=null){
                openApi.getTags().forEach(tag -> {
                    Map<String,Object> map=new HashMap<>();
                    map.put("x-order", RandomUtil.randomInt(0,100));
                    tag.setExtensions(map);
                });
            }
            if(openApi.getPaths()!=null){
                openApi.addExtension("x-test123","333");
                openApi.getPaths().addExtension("x-abb",RandomUtil.randomInt(1,100));
            }
        };
    }

    @Bean
    public OpenAPI customOpenAPI() {
        log.info("初始化swagger配置");
        return new OpenAPI()
                .info(new Info()
                        .title("基于SpringBoot的OA办公系统")
                        //作者
                        // 配置作者信息
                        .contact(new Contact().name("软件21-4尤如旭").email("2322589689@qq.com"))
                        .version("1.0")
                        .description( "课题来源于自命题。在当今数字化办公的时代浪潮下，企业对于高效、便捷、智能化的办公管理需求极为迫切。随着信息" +
                                "技术的迅猛发展，传统办公模式逐渐向自动化办公转型，以提升办公效率、优化资源配置并降低管理成本。预期本课题的研究与实践能" +
                                "够为企业办公提供一套集成化的解决方案，极大地提高办公流程的规范化与效率，减少人为错误与资源浪费，" +
                                "增强企业内部信息流通与协同办公能力，助力企业在激烈的市场竞争中提升管理效能与竞争力，推动技术创新与实践发展。")
                        .termsOfService("http://doc.xiaominfo.com")
                        .license(new License().name("Apache 2.0")
                                .url("http://doc.xiaominfo.com")));
    }


}
